package jia.core.calculations;

import jia.game.GenericFlags;
import org.apache.log4j.Logger;

import java.awt.*;
import java.util.ArrayList;

/**
 * Class that handles aspects of defining an area.  Each area is a square that is immediately surrounding
 * a given Space.
 * @see net.sf.jia.framework.Space
 */
public class Area {
    private static final Logger log = Logger.getLogger(Area.class);
    
    /**
     * Returns an arraylist of spaces immediately surrounding the specific space (or origin) passed
     * to the method.  Spaces are added to the list in N/S/E/W, NW/NE/SW/SE order.
     *
     * @param origin
     * @return
     */
    public ArrayList<Point> getSurroundingSquares(Point origin) {
        ArrayList<Point> surroundingSquares = new ArrayList<Point>();
        
        double increment = 1;
        double decrement = -1;
        
        //top square
        Point topSquare = new Point();
        topSquare.setLocation(origin.getX(), origin.getY() + decrement);
        surroundingSquares.add(topSquare);
        
        //bottom square
        Point bottomSquare = new Point();
        bottomSquare.setLocation(origin.getX(), origin.getY() + increment);
        surroundingSquares.add(bottomSquare);
        
        //left square
        Point leftSquare = new Point();
        leftSquare.setLocation(origin.getX() + decrement, origin.getY());
        surroundingSquares.add(leftSquare);
        
        //right square
        Point rightSquare = new Point();
        rightSquare.setLocation(origin.getX() + increment, origin.getY());
        surroundingSquares.add(rightSquare);
        
        //NW Square
        Point nwSquare = new Point();
        nwSquare.setLocation(origin.getX() + decrement, origin.getY() + decrement);
        surroundingSquares.add(nwSquare);
        
        //NE Square
        Point neSquare = new Point();
        neSquare.setLocation(origin.getX() + increment, origin.getY() + decrement);
        surroundingSquares.add(neSquare);
        
        //SW Square
        Point swSquare = new Point();
        swSquare.setLocation(origin.getX() + decrement, origin.getY() + increment);
        surroundingSquares.add(swSquare);
        
        //SE Square
        Point seSquare = new Point();
        seSquare.setLocation(origin.getX() + increment, origin.getY() + increment);
        surroundingSquares.add(seSquare);
        
        
        return surroundingSquares;
    }
    
    /**
     * Method that determines if the opponent is within an acceptable range to make a melee attack.
     * The origin point determines where the attack is coming from.  The arraylist is a collection of
     * squares that surrounds the attacking character.  Each space is evaluated to determine if the opponent
     * occupies it.  If any of those conditions return true, then the opponent is within range.
     *
     * @param origin
     * @param destination
     * @param squareRange
     * @return
     */
    public boolean isInMeleeRange(Point origin, Point destination, ArrayList squareRange) {
        boolean retVal = false;
        if (GenericFlags.debugMessages) {
            log.debug("Checking opponents surrounding squares in relation to this one " + squareRange.size());
        }
        //make sure they do not occupy the same space
        if (origin.getX() != destination.getX() && origin.getY() != destination.getY()) {
            for (Object aSquareRange : squareRange) {
                Point testPoint = (Point) aSquareRange;
                if (GenericFlags.debugMessages) {
                    log.debug("Origin X " + origin.getX() + " / Test X " + testPoint.getX() + "  --  Origin Y " + origin.getY() + " / Test Y " + testPoint.getY());
                }
                if (origin.getX() == testPoint.getX()) {
                    if (GenericFlags.debugMessages) {
                        log.debug("The X Square matched!");
                    }
                    if (origin.getY() == testPoint.getY()) {
                        if (GenericFlags.debugMessages) {
                            log.debug("The Y square matched");
                        }
                        retVal = true;
                        break;
                    }
                }
            }
        } else {
            if (GenericFlags.debugMessages) {
                log.debug("Characters occupy the same space");
            }
            retVal = true;
        }
        
        return retVal;
    }
}
